package com.linkage.jz.bankruptcy.widget;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.linkage.jz.bankruptcy.R;

public class CommonPromptDialogTep extends Dialog {

    public final static int TYPE_TIPS = 1; // 提示、确认
    public final static int TYPE_EDIT_SINGLE = 2; // 单行输入
    public final static int TYPE_EDIT_MULT = 3; // 多行输入

    private Activity context;
    private int mType;
    private Button dialog_btn_negative, dialog_btn_positive;
    private TextView dialog_prompt_content, dialog_prompt_title;
    public EditText dialog_prompt_edit_single;
    public CountEditText dialog_prompt_edit;
    private String editHint;
    private int maxLenght = 0;
    private String title, content, negativeStr, positiveStr;
    private View.OnClickListener mCloseListener;
    private View.OnClickListener mNegativeClickListener, mPositiveClickListener;

    private Boolean isCenter =false;
    public CommonPromptDialogTep(Activity context, int type) {
        super(context);
        this.context = context;

        mType = type;
    }
    public void setIsCenter(boolean iscenter) {
        isCenter = iscenter;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_prompt_dialog_tmp);

        Window dialogWindow = this.getWindow();

        WindowManager m = context.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (d.getWidth() * 0.99); // 宽度设置为屏幕的0.8
        dialogWindow.setAttributes(p);
        dialog_prompt_title = findViewById(R.id.dialog_prompt_title);

        dialog_prompt_content = findViewById(R.id.dialog_prompt_content);
        if(isCenter)
        {
            dialog_prompt_content.setGravity(Gravity.CENTER_HORIZONTAL);
        }
        dialog_prompt_edit_single = findViewById(R.id.dialog_prompt_edit_single);
        dialog_prompt_edit = findViewById(R.id.dialog_prompt_edit);
        if (maxLenght > 0) {
            dialog_prompt_edit.setmMaxLength(maxLenght);
        }
        dialog_btn_negative = findViewById(R.id.dialog_btn_negative);
        dialog_btn_positive = findViewById(R.id.dialog_btn_positive);

        if (mCloseListener != null) {
            findViewById(R.id.dialog_prompt_close).setOnClickListener(mCloseListener);
        } else {
            findViewById(R.id.dialog_prompt_close).setOnClickListener(view -> dismiss());
        }


        if (!TextUtils.isEmpty(title)) {
            dialog_prompt_title.setText(title);
        }
        if (mType == TYPE_TIPS) {
            dialog_prompt_content.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(content)) {
                dialog_prompt_content.setText(content);
            }
        }

        if (mType == TYPE_EDIT_SINGLE) {
            dialog_prompt_edit_single.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(content)) {
                dialog_prompt_edit_single.setHint(editHint);
            }
        }
        if (mType == TYPE_EDIT_MULT) {
            dialog_prompt_edit.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(editHint)) {
                dialog_prompt_edit.setHint(editHint);
            }
        }
//        if (!TextUtils.isEmpty(editHint)) {
//            dialog_prompt_edit.setHint(editHint);
//            dialog_prompt_edit.setVisibility(View.VISIBLE);
//        }


        if (!TextUtils.isEmpty(negativeStr)) {
            dialog_btn_negative.setText(negativeStr);
        }
        if (!TextUtils.isEmpty(positiveStr)) {
            dialog_btn_positive.setText(positiveStr);
        }
        if (mPositiveClickListener != null) {
            dialog_btn_positive.setOnClickListener(mPositiveClickListener);
        } else {
            dialog_btn_positive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                }
            });
        }
        if (mNegativeClickListener != null) {
            dialog_btn_negative.setOnClickListener(mNegativeClickListener);
        } else {
            dialog_btn_negative.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                }
            });
        }
        // 确定、取消都没有事件，那就是显示个提示信息，右上角叉掉就行了
        if (mPositiveClickListener == null && mNegativeClickListener == null) {
            findViewById(R.id.area_btn).setVisibility(View.GONE);
        }
        this.setCancelable(true);
    }

    public void setmCloseListener(View.OnClickListener clickListener) {
        this.mCloseListener = clickListener;
    }

    public void setNegativeClickListener(String text, View.OnClickListener clickListener) {
        negativeStr = text;
        mNegativeClickListener = clickListener;
    }


    public void setPositiveClickListener(String text, View.OnClickListener clickListener) {
        positiveStr = text;
        mPositiveClickListener = clickListener;
    }

    public void setTitle(String text) {
        title = text;
    }

    public String getTitle() {
        return title;
    }

    public void setContent(String text) {
        content = text;
    }

    public void setEditHint(String text) {
        editHint = text;
    }

    public void setEditMaxLength(int num) {
        maxLenght = num;
    }
}


